package mbp.gui.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import mbp.core.ResourceValidation;
import mbp.gui.base.IListPanel;
import mbp.message.validation;

public class ValidationListPanel extends JPanel implements IListPanel {
	private JTable RcvTable;
	private DefaultTableModel RcvModel;

	private List<validation> rcviList;
	private List<ResourceValidation> rcvList;

	/**
	 * Create the panel.
	 */
	public ValidationListPanel() {
		setLayout(new BorderLayout(0, 0));
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);

		RcvModel = new DefaultTableModel(new Object[][] {}, new String[] {
				"Type", "Condition" });

		RcvTable = new JTable() {
			@Override
			public boolean isCellEditable(int row, int column) {
				// set table column uneditable
				return false;
			}
		};

		RcvTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		RcvTable.setFillsViewportHeight(true);
		RcvTable.setToolTipText("<html>Double click for ResourceValidation details<BR>Press DEL to remove</html>");
		RcvTable.setModel(RcvModel);

		// MyTableCellRenderer mtcr = new MyTableCellRenderer();
		// RcvTable.setDefaultRenderer(Object.class,mtcr);
		scrollPane.setViewportView(RcvTable);

		// podwojne klikniecie na tabele i pobranie indeksu zaznaczonego wiersza
		RcvTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					processDoubleClick(RcvTable.getSelectedRow());
				}
			}
		});

		RcvTable.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyCode() == KeyEvent.VK_DELETE)
					processKeyPressed(RcvTable.getSelectedRow());
			}
		});

	}

	public void processDoubleClick(int index) {
		System.out.println(" double click: selected row: "
				+ RcvTable.getSelectedRow());
	}

	public void processKeyPressed(int index) {
		System.out.println(" key pressed: selected row: "
				+ RcvTable.getSelectedRow());
	}

	public void updateCondition(int index, String condition) {
		RcvModel.setValueAt(condition, index, 1);
	}

	public JTable getTable() {
		return RcvTable;
	}

	private void reloadInfo() {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				int rows = 0;
				do {
					rows = RcvModel.getRowCount();
					if (rows > 0) {
						RcvModel.removeRow(0);
					}
				} while (rows > 0);

				for (int i = 0; i < rcviList.size(); i++) {
					String type = rcviList.get(i).type;
					String cnd = rcviList.get(i).condition;
					RcvModel.insertRow(RcvModel.getRowCount(), new Object[] {
							type, cnd });

				}

			}
		});
	}

	private void reloadObj() {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				int rows = 0;
				do {
					rows = RcvModel.getRowCount();
					if (rows > 0) {
						RcvModel.removeRow(0);
					}
				} while (rows > 0);

				for (int i = 0; i < rcvList.size(); i++) {
					String type = rcvList.get(i).getType()
							.getResourceTypeName();
					String cnd = rcvList.get(i).getCondition();
					RcvModel.insertRow(RcvModel.getRowCount(), new Object[] {
							type, cnd });

				}
			}
		});
	}

	@Override
	public void reloadList() {
		if (rcvList != null) {
			reloadObj();
		}
		if (rcviList != null) {
			reloadInfo();
		}

	}

	@Override
	public <validation> void reloadListInfo(List<validation> infoList) {
		this.rcviList = (List<mbp.message.validation>) infoList;
		this.rcvList = null;
		reloadInfo();
	}

	@Override
	public <ResourceValidation> void reloadListObj(
			List<ResourceValidation> objList) {

		this.rcvList = (List<mbp.core.ResourceValidation>) objList;
		this.rcviList = null;
		reloadObj();
	}

	// public class MyTableCellRenderer extends DefaultTableCellRenderer
	// {
	// public Component getTableCellRendererComponent( JTable table,
	// Object value,
	// boolean isSelected,
	// boolean hasFocus,
	// int row,
	// int column )
	// {
	// Component c = super.getTableCellRendererComponent(table, value,
	// isSelected, hasFocus,
	// row, column);
	// if(column == 0)
	// {
	// String svalue = (String) value;
	// if(svalue.compareTo("WOOD")==0)
	// {
	// c.setBackground(Color.magenta);
	// }
	// }
	// else
	// {
	// c.setBackground(Color.WHITE);
	// }
	//
	// return c;
	//
	// }
	// }
}
